package com.example.smartcitydemo.ui.smartbus.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseExpandableListAdapter
import androidx.databinding.DataBindingUtil
import com.example.smartcitydemo.R
import com.example.smartcitydemo.databinding.ItemSmartBusChildBinding
import com.example.smartcitydemo.databinding.ItemSmartBusParentBinding
import com.example.smartcitydemo.ui.smartbus.bean.SmartBusExpanableData


/**
 * @PACKAGE_NAME: com.example.smartcity_jscs.ui.smartbus.adapter
 * @NAME: Caleb.F
 * @DATE: 2021/8/24 10:05
 * @Email: 1919796283@qq.com
 */
class SmartBusExpanableAdapter(
    var list: List<SmartBusExpanableData> = mutableListOf()
) :
    BaseExpandableListAdapter() {

    override fun getGroupCount(): Int {
        return list.size
    }

    override fun getChildrenCount(groupPosition: Int): Int {
        return list[groupPosition].childData.size
    }

    override fun getGroup(groupPosition: Int): Any {
        return list[groupPosition]
    }

    override fun getChild(groupPosition: Int, childPosition: Int): Any {
        return list[groupPosition].childData[childPosition]
    }

    override fun getGroupId(groupPosition: Int): Long {
        return groupPosition.toLong()
    }

    override fun getChildId(groupPosition: Int, childPosition: Int): Long {
        return childPosition.toLong()
    }

    override fun hasStableIds(): Boolean {
        return true
    }

    override fun getGroupView(
        groupPosition: Int,
        isExpanded: Boolean,
        convertView: View?,
        parent: ViewGroup?
    ): View {
        val parentBinding: ItemSmartBusParentBinding = if (convertView == null) {
            DataBindingUtil.inflate(
               LayoutInflater.from(parent!!.context),
                R.layout.item_smart_bus_parent,
                parent,
                false
            )
        } else {
            DataBindingUtil.getBinding(convertView)!!
        }
        bindingParent(parentBinding, groupPosition)
        if (isExpanded) {
            parentBinding.ivSmartBusRight.animate().rotation(-90f).setDuration(500).start()
        } else {
            parentBinding.ivSmartBusRight.animate().rotation(90f).setDuration(500).start()
        }
        return parentBinding.root
    }


    override fun getChildView(
        groupPosition: Int,
        childPosition: Int,
        isLastChild: Boolean,
        convertView: View?,
        parent: ViewGroup?
    ): View {
        val childBinding: ItemSmartBusChildBinding = if (convertView == null)
            DataBindingUtil.inflate<ItemSmartBusChildBinding>(
                LayoutInflater.from(parent!!.context),
                R.layout.item_smart_bus_child,
                parent,
                false
            )
        else DataBindingUtil.findBinding(convertView)!!
        bingChild(childBinding, groupPosition, childPosition)
        return childBinding.root
    }


    override fun isChildSelectable(groupPosition: Int, childPosition: Int): Boolean {
        return true
    }

    /**
     * 父节点绑定数据
     * @param parentBindding ItemSmartBusParentBinding
     */
    private fun bindingParent(parentBinding: ItemSmartBusParentBinding, groupPosition: Int) {
        val busListInfo = list[groupPosition].parentData
        parentBinding.apply {
            tvSmartBusMile.text = busListInfo.miledgeString
            tvSmartBusPricce.text = busListInfo.priceString
            tvSmartBusStart.text = busListInfo.first
            tvSmartBusEnd.text = busListInfo.end
            button.text = busListInfo.name
            tvSmartBusTime.text = busListInfo.updateTime
        }
    }

    /**
     * 子节点绑定数据
     * @param childBinding ItemSmartBusChildBinding
     */
    private fun bingChild(
        childBinding: ItemSmartBusChildBinding,
        groupPosition: Int,
        childPosition: Int
    ) {
        val name = list[groupPosition].childData[childPosition].name
        childBinding.tvSmartBus.text = name
    }


}